/**
 * 游戏场玩家界面
 */
class DDZPlayerView extends V.PlayerView 
{
    public avatarImg: eui.Image;
    public moneyGroup: eui.Group;
    public moneyLabel: eui.BitmapLabel;
    public nickNameLabel: eui.Label;
    public readyIcon: eui.Image;
    public isRight: boolean = false;
    public layerGroup: eui.Group;
    public clockGroup: eui.Group;
    public clockLabel: eui.Label;
    public putCardList: eui.List;
    public cardLengthGroup: eui.Group;
    public cardLength: eui.BitmapLabel;
    public isLordIcon: eui.Image;
    public chatBg: eui.Image;
    public chatLabel: eui.Label;
    public chatGroup: eui.Group;
    public faceImg: eui.Image;
    public headGroup: eui.Group;
    public offLineGroup: eui.Group;

    private clockTimeID: number;
    private time: number;
    private chatHideTimeID: number;

    public show(): boolean 
    {
        super.show();
        this.initUI();
        return true;
    }

    public initUI(): void
    {
        if (this.isRight) 
        {
            this.layerGroup.right = undefined;
            this.layerGroup.left = 0;
            this.readyIcon.left = undefined;
            this.readyIcon.right = 0;
            this.putCardList.left = undefined;
            this.putCardList.right = 0;
            this.cardLengthGroup.left = undefined;
            this.cardLengthGroup.right = 0;
            this.chatGroup.left = undefined;
            this.chatGroup.right = 0;
            this.faceImg.left = undefined;
            this.faceImg.right = 0;
            this.chatBg.scaleX = -1;
            var layout: eui.TileLayout = this.putCardList.layout as eui.TileLayout;
            layout.horizontalAlign = "right";
        }
        this.cardLengthGroup.visible = false;
        this.readyIcon.visible = false;
        this.chatGroup.visible = false;
        console.log("this.chairId:", this.chairId);
        console.log(D.App.table.players[this.chairId]);
        this.setSitUI(D.App.table.players[this.chairId]);
    }

    private setSitUI(data: any): void
    {
        if(data)
        {
            this.nickNameLabel.text = base.Utils.formatNickName(data.nickName, 10); //base.Utils.threePoint(data.nickName, this.nickNameLabel.width);
            this.moneyLabel.text = base.Utils.formatCurrency(data.score);
            this.readyIcon.visible = false;
            this.clockGroup.visible = false;
            this.offLineGroup.visible = false;
            this.setAvatar(false, false, data.gender, data.faceID);
        }
    }

    public onSyncData(d: any): void
    {
        let gameStatus = D.App.table.gameStatus;
        if(gameStatus == 103)
        {
            this.cardLength.text = d.bCardCount[this.chairId] + "";
            if (!this.isMe() && d.wCurrentUser == this.chairId) //如果轮到自己出牌了
            {
                this.setOperateUI(false);
            }
        }
    }


    public onPlayerCall(d: any, isRob): void
    {
        var isCall = d.bLandScore == 1;
        var operateChair = d.bLandUser;
        if(isRob)
        {
            operateChair = d.wDoubleUser;
            isCall = d.bDoubleScore == 1;
        }
        if(operateChair == this.chairId)
        {
            if (!this.isMe())
            {
                this.setCallUI(isCall, isRob);
            }
            this.setOperateOverUI();
            var sexString = this.getData().gender == 1 ? "Man" : "Woman";
            if (!isRob)
            {
                how.SoundManager.playEffect(how.StringUtils.format(isCall ? SoundConfig.GAME_LORD_CALL : SoundConfig.GAME_LORD_NOTCALL, sexString));
            }
            else
            {
                if (isCall)
                {
                    how.SoundManager.playEffect(how.StringUtils.format(SoundConfig.GAME_QIANGLORD_CALL, sexString, Math.round(how.Random.range(1, 3))));
                }
                else 
                {
                    how.SoundManager.playEffect(how.StringUtils.format(SoundConfig.GAME_QIANGLORD_NOTCALL, sexString));
                }
            }
        }
        if (!this.isMe() && d.wCurrentUser == this.chairId) 
        {
            this.setOperateUI(true);
        }
        // this.data.isLord = data.lord == this.data.chair;//是否是地主设置给数据
        // if (data.cards) 
        // {
        //     //如果有地主产生
        //     this.data.isPlaying = true;
        //     this.callUI("setAvatar", false, false, this.data.sex, this.data.avatar);
        // }
        // if (data.cards) 
        // {
        //     //如果有地主产生
        //     var isLord: boolean = data.lord == this.data.chair;
        //     if (isLord) {//如果自己是地主
        //         this.setOperateUI(false, GameConfig.lordPutCardTime);
        //     }
        //     this.setLordUI();
        // }
    }

    public setCallUI(isCall: boolean, isRob: boolean): void 
    {
        this.readyIcon.visible = true;
        var jiaodizhuSource = !isRob ? "ddz_jiaodizhu_png" : "ddz_qiangdizhu_png";
        var buqiangSource = !isRob ? "ddz_bujiao_png" : "ddz_buqiang_png";
        this.readyIcon.source = isCall ? jiaodizhuSource : buqiangSource;
    }

    public onBrightCard(d: any): void
    {
        this.readyIcon.visible = false;
    }

    public setAvatar(isLord: boolean, isTuoguan: boolean, sex: number, avatar: string): void 
    {
        this.isLordIcon.source = isLord ? "ddz_dizhu_png" : "ddz_nongming_png";
        if (isTuoguan) 
        {
            this.avatarImg.source = "ddz_tuoguan_png";
        }
        else 
        {
            if (isLord) 
            {
                this.avatarImg.source = "ddz_" + (sex == 0 ? "Woman" : "Man") + "_dz_png";
            }
            else 
            {
                this.avatarImg.source = "ddz_" + (sex == 0 ? "Woman" : "Man") + "_1_png";
            }
        }
    }
    

    /**
     * 确定地主
     */
    public setLordUI(data: any): void 
    {
        this.readyIcon.visible = false;
        this.cardLength.text = "20";
        this.isLordIcon.visible = true;
        this.isLordIcon.source = "ddz_dizhu_png";
        this.setAvatar(true, false, data.gender, data.faceID);
    }

    private startClock(startTime: number): void 
    {
        this.time = startTime;
        this.stopClock();
        this.clockGroup.visible = true;
        this.clockLabel.text = startTime + "";
        this.clockTimeID = egret.setInterval(this.onClock, this, 1000);
    }

    private stopClock(): void 
    {
        egret.clearInterval(this.clockTimeID);
        this.clockGroup.visible = false;
    }

    private onClock(): void 
    {
        var current: number = parseInt(this.clockLabel.text);
        if (current > 1) 
        {
            this.clockLabel.text = current - 1 + "";
        }
        else 
        {
            this.onClockOver();
        }
    }

    /**
    * 倒计时结束
    */
    public onClockOver(): void 
    {
        this.stopClock();
        this.startClock(this.time);
    }

  
    public setGameStartUI(data: any, cardLength: number = 17, isReconnect: boolean = false): void 
    {
        this.readyIcon.visible = false;
        this.putCardList.dataProvider = null;
        this.cardLengthGroup.visible = !this.isMe();
        if (!isReconnect) {
            this.cardLength.text = "0";
            egret.Tween.get(this.cardLength).to({ text: cardLength }, 2000);
        }
        else {
            this.cardLength.text = cardLength.toString();
        }
        // this.cardLength.text = cardLength + "";
    }

    public onPlayerPut(d: any): void 
    {
        if (d.wOutCardUser == this.chairId)  //如果是自己出的牌
        {
            this.setOperateOverUI();
            if (!this.isMe()) //如果出牌了
            {
                this.setPutCardUI(d.bCardData ? d.bCardData : null, true, this.getData().gender, false);
            }
        }
        var current: number = d.wCurrentUser;
        if (!this.isMe() && current == this.chairId) //如果轮到自己出牌了
        {
            this.setOperateUI(false);
        }
    }

    public onPlayerPass(d: any):void
    {
        if (d.bNewTurn == 1) 
        {
            //如果一轮结束
            this.setRoundOverUI();
        }
        if (d.wPassUser == this.chairId)  //如果是自己pass
        {
            this.setOperateOverUI();
            if (!this.isMe()) //如果出牌了
            {
                this.setPutCardUI(null, true, this.getData().gender, false);
            }
        }
        var current: number = d.wCurrentUser;
        if (!this.isMe() && current == this.chairId) //如果轮到自己出牌了
        {
            this.setOperateUI(false);
        }
    }

        //设置操作UI
    public setOperateUI(isCall: boolean, remainTime: number = -1): void 
    {
        if (!this.isMe()) 
        {
            var time: number = isCall ? GameConfig.callTime : GameConfig.putCardTime;
            if (remainTime != -1) 
            {
                time = remainTime;
            }
            this.startClock(time);
            this.putCardList.dataProvider = null;
            this.readyIcon.visible = false;
        }
    }

    public setPutCardUI(cards: number[], needRemove: boolean, sex: number, isReconnect: boolean): void 
    {
        if (cards) 
        {
            if (!isReconnect) 
            {
                how.SoundManager.playEffect(SoundConfig.GAME_CHUPAI);
            }
            how.poker.CardLogic_DDZ3.sortByCardData(cards);
            this.putCardList.dataProvider = new eui.ArrayCollection(cards);
            this.onPutCardListChange();
            this.readyIcon.visible = false;
            if (needRemove) 
            {
                this.cardLength.text = parseInt(this.cardLength.text) - cards.length + "";
                var cardCount = parseInt(this.cardLength.text);
                if (cardCount <= 2 && cardCount > 0) // 小于3张牌开始报警
                {
                    how.SoundManager.playEffect(SoundConfig.GAME_RING);
                    var sexString = sex == 1 ? "Man" : "Woman";
                    how.SoundManager.playEffect(how.StringUtils.format(SoundConfig.GAME_RING_CALL, sexString, cardCount));
                }
            }
        }
        else 
        {
            this.putCardList.dataProvider = null;
            this.readyIcon.visible = true;
            this.readyIcon.source = "ddz_buchu_png";
        }
    }

    private onPutCardListChange(): void 
    {
        var cardListData = this.putCardList.dataProvider as eui.ArrayCollection;
        var layout;
        if (cardListData && cardListData.length <= 8) {
            layout = new eui.HorizontalLayout();
            layout.gap = -80;
        }
        else {
            layout = new eui.TileLayout();
            layout.horizontalGap = -80;
            layout.verticalGap = -100;
            layout.requestedColumnCount = 8;
        }
        this.putCardList.layout = layout;
    }

    public setOperateOverUI(): void {
        this.stopClock();
    }

    /**
     * 一轮结束
     */
    public setRoundOverUI(): void 
    {
        // this.callUI("setRoundOverUI");
    }
    
    public onDestroy(): void 
    {
        egret.clearTimeout(this.chatHideTimeID);
        egret.Tween.removeTweens(this);
        egret.Tween.removeTweens(this.cardLength);
        this.stopClock();
    }
}